﻿using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors.Sensors;
using Newtonsoft.Json.Linq;
using System.Collections.Concurrent;


namespace Awsc.DataAcquisitionSys.Core.DataAcquisition
{
    /// <summary>
    /// 传感器数据快照，存储当前传感器数据。
    /// </summary>
    public class GlobalDeviceManager
    {
        private GlobalDeviceManager()
        {

        }
        private static GlobalDeviceManager _instance = new GlobalDeviceManager();
        public static GlobalDeviceManager Instance => _instance;

        public readonly ConcurrentDictionary<string, Sensor> _entryValues = new ConcurrentDictionary<string, Sensor>();

        public readonly ConcurrentDictionary<string, DataCollector> _dataCollectors = new ConcurrentDictionary<string, DataCollector>();


        public void SetValue(Sensor value)
        {
            if (value == null)
            {
                return;
            }
            _entryValues[value.Name] = value;
        }

        public void SetValue(string name, object value)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }
            if (value == null)
            {
                return;
            }
            if (_entryValues.ContainsKey(name))
            {
                _entryValues[name].OutputValue = value;
            }

        }


        public void SetValues(IEnumerable<Sensor> values)
        {
            if (values == null || !values.Any())
            {
                return;
            }
            foreach (var item in values)
            {
                SetValue(item);
            }
        }


        public void AddCollector(DataCollector dataCollector)
        {
            if (dataCollector == null || string.IsNullOrWhiteSpace(dataCollector.CollectorConfig?.Name))
            {
                return;
            }
            _dataCollectors[dataCollector.CollectorConfig.Name] = dataCollector;
            
            SetValues(dataCollector.Sensors);
        }

        public void RemoveCollector(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }
            _dataCollectors.TryRemove(name, out var _);
        }



        public Sensor GetSensor(string entryName)
        {
            if (string.IsNullOrEmpty(entryName) || !_entryValues.ContainsKey(entryName))
            {
                return null;
            }
            return _entryValues[entryName];
        }

        public SensorData GetSensorValue(string sensorName)
        {
            if (string.IsNullOrEmpty(sensorName) || !_entryValues.ContainsKey(sensorName))
            {
                return null;
            }
            if (sensorName == "悬重")
            {

            }
            var sensor = _entryValues[sensorName];
            return new SensorData()
            {
                Name = sensor.Name,
                Unit = sensor.Unit,
                Value = sensor.CurrentValue,
                Status = sensor.Status
            };
        }

        public List<Sensor> GetAllSensors()
        {
            return _entryValues.Values.ToList();
        }
        public List<SensorData> GetAllSensorData()
        {
            List<SensorData> result = new List<SensorData>();
            foreach (var sensor in _entryValues.Values)
            {
                result.Add(new SensorData()
                {
                    Name = sensor.Name,
                    Value = sensor.CurrentValue,
                    Unit = sensor.Unit,
                    Status = sensor.Status
                });
            }
            return result;
        }

        public List<Sensor> GetSensors(IEnumerable<string> sensorNames)
        {
            List<Sensor> result = new List<Sensor>();
            foreach (var sensorName in sensorNames)
            {
                if (_entryValues.TryGetValue(sensorName, out var value))
                {
                    result.Add(value);
                }
            }
            return result;
        }
        public List<SensorData> GetSensorValues(IEnumerable<string> sensorNames)
        {
            List<SensorData> result = new List<SensorData>();
            foreach (var sensorName in sensorNames)
            {
                if (_entryValues.TryGetValue(sensorName, out var value))
                {
                    result.Add(new SensorData()
                    {
                        Name = value.Name,
                        Value = value.CurrentValue,
                        Unit = value.Unit,
                        Status = value.Status


                    });
                }
                else
                {
                    result.Add(null);
                }
            }
            return result;
        }
    }





}
